package com.android.tnovoselec.travelassistant.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

import com.android.tnovoselec.travelassistant.R;
import com.android.tnovoselec.travelassistant.dto.MeteoFavoritesDto;

/**
 * Title: Android Framework - Meteo <br />
 * <br />
 * Copyright: Copyright (c) 2011 <br />
 * <br />
 * NTH Company: NTH d.o.o. <br />
 * <br />
 * 
 * MeteoAdapter displays data on {@link ViewPager} with corresponding weather
 * details.
 * 
 * @author Tomislav Novoselec
 * @version 1.0.0
 * @since 1.0.0
 * 
 * 
 */

public class MeteoAdapter extends PagerAdapter {

    private List<MeteoFavoritesDto> data;
    private Context context;

    public MeteoAdapter(Context context) {
	super();
	this.data = new ArrayList<MeteoFavoritesDto>();
	this.context = context;
    }

    public void setData(List<MeteoFavoritesDto> data) {
	if (data == null) {
	    this.data.clear();
	} else {
	    this.data = data;
	}
	notifyDataSetChanged();
    }

    @Override
    public int getCount() {
	return data.size();
    }

    @Override
    public void startUpdate(View container) {
	// TODO Auto-generated method stub

    }

    @Override
    public Object instantiateItem(View container, int position) {
	View root = LayoutInflater.from(context).inflate(R.layout.meteo_details, (ViewGroup) container, false);

	MeteoFavoritesDto dataDetails = data.get(position);

	((TextView) root.findViewById(R.id.meteo_details_description)).setText(dataDetails.getConditionString());
	((TextView) root.findViewById(R.id.meteo_details_current_temp_value)).setText("" + dataDetails.getTemperature() + context.getString(R.string.meteo_temperature_degree));
	((TextView) root.findViewById(R.id.meteo_details_current_wind_value)).setText("" + dataDetails.getWindSpeed() + " " + dataDetails.getWindSpeedUnit() + " " + dataDetails.getWindDirection());
	((TextView) root.findViewById(R.id.meteo_details_current_humidity_value)).setText("" + dataDetails.getHumidity());
	((ViewPager) container).addView(root, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
	return root;
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
	((ViewPager) container).removeView((View) object);

    }

    @Override
    public void finishUpdate(View container) {
	// TODO Auto-generated method stub

    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
	return object == view;
    }

    @Override
    public Parcelable saveState() {
	return null;
    }

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
	// TODO Auto-generated method stub

    }

    @Override
    public CharSequence getPageTitle(int position) {
	return data.get(position).getTitle();
    }

}
